﻿var manager = null;
var dataUrl = "../ProductType/GetJsonOfProductType?rand=" + Math.random();
$(function () {
    menuInit();
    InitProductTypeTreeData();
});
function InitTreeData() {
    window.location.href = "../ProductType/Index";
 }
function InitProductTypeTreeData() {

//     $.get(url, {}, function (productTypeDate) {
 //        if (productTypeDate != undefined) {
             if (manager != null && manager != undefined) { manager.clear(); }
             manager = $(".l-tree").ligerTree({
                 url: dataUrl+"&isRoot=true",
                 checkbox: false,
                 nodeWidth: 100,
                 onBeforeExpand: onBeforeExpand,
                 onClick: itemclick
             });
             $("#initLoadingImg").css("display", "none");

 //        }
//     });
         }

//
         function onBeforeExpand(note) {
            // itemclick(note.data, 0);
             if (note.data.children && note.data.children.length == 0) {
                 //         if ($("#getnodeisorg").val() == "0") {
                 //这里模拟一个加载节点的方法，append方法也用loadData(target,url)代替
                 var parentId = note.data.id;
                 if (parentId != null && parentId != undefined) {
                     $.get(dataUrl + "&isRoot=false", { parentId: parentId }, function (data) {
                         if (data != undefined && data != null && data.length > 0) {
                             //---------------------
                             manager.append(note.target, data);
                             //----------------------
                         }
                     });
                 }
                 //         }
             }
         }

 function itemclick(item, i) {
     if (item.data != null && item.data != undefined) {
         $("#getnodeid").val(item.data.id);
         $("#getnodename").val(item.data.text);
         $("#getnodeisorg").val(item.data.isorg);
     }
 }


 //首页 toolbar

 function menuInit() {
     $("#toptoolbar").ligerToolBar({ items: [
                { text: '新增', click: addOperation, icon: 'add' },
                { line: true },
                 { text: '修改', click: editOperation, icon: 'modify' },
                { line: true },
                { text: '删除', click: DeleteOperation, img: '../Content/images/delete.gif' }
            ]
     });
 }
 var id;
 var name;
 var isorg;
 function IsSelectOneNode()
 {
      id = $("#getnodeid").val();
      name = $("#getnodename").val();
      isorg = $("#getnodeisorg").val();
     var tipinfo="";
     if (id == undefined || id == null || $.trim(id.toString()).length < 1) {
         tipinfo="请选择一个结点！";
     } 
     if(tipinfo.length>0){
         $.ligerDialog.success(tipinfo); return false;
        }
        return true;
 }
 //
 //add
 function addOperation() {
     var selectOneNode = false;
     id = $("#getnodeid").val();
     $.ligerDialog.open({ url: encodeURI("../ProductType/Add?id=" + id), height: getDialogHeight(), width: getDialogWidth(),
         isResize: true, isHidden: false
     });

 }

 //
 function editOperation() {
     if (IsSelectOneNode() == true) {
         $.ligerDialog.open({ url: encodeURI("../ProductType/Edit?id=" + id.toString() ), height: getDialogHeight(), width: getDialogWidth(),
             isResize: true, isHidden: false
         });
     }
 }

 function DeleteOperation() {
     if (IsSelectOneNode() == true) {
         $.ligerDialog.confirm('确定要删除吗？', function (yes) {
             if (yes) {
                 var deleteUrl = "../ProductType/Delete?id=" + id.toString();
                 $.get(deleteUrl, { "id": id.toString() }, function (data) {
                     var tip = "";
                     if (data != undefined && data == "ok") {
                         tip = "操作成功！";
                         InitTreeData();
                     }
                     else {
                         tip = "操作失败！";
                     }
                     $.ligerDialog.success(tip); return false;
                 });
             } 
         });
     }
 }


 ///清空treeNode
 function removeAll(treeId)  
 {  
  var s=$("#"+treeId);  
  var t=s.childNodes.length;  
  for (var i=0;i<t;i++)  
  {  
    s.removeChild(s.childNodes[i]);  
  }  
 } 